define(["jquery", "bbcare",
    "scprit/followup/app/datatable/datatable.servers.js",
    "scprit/followup/app/datatable/datatable.ui.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningList.ui.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningList.event.js"
], function ($, $bb, $datatableServers, $datatableUi, $ui, $dataDeal, $event) {
    var servers = {}
    var URL = $bb.baseUrl("followup") + "followup/service"
    /**
     * 入参
     */
    var paramsDeal = function (type) {
        var inParams = function (args) {
            var data = JSON.stringify({
                data: {
                    "args": JSON.stringify(args),
                    'examinationPaperName': type,
                    'phoneNum': $.trim($("#tel").val()),
                    'patientName': $.trim($("#username").val())
                }
            })
            return data
        }
        return inParams
    }
    /**
     * 出参
     */
    var paramsDeal2 = function (type) {
        var outParams = function (data) {
            if (data.rows == 0) {
                var phone = $.trim($("#tel").val())
                var name = $.trim($("#username").val())
                if (phone) {
                    $("#newTel").addClass("show")
                }
                if (name) {
                    $("#newName").addClass("show")
                }
            } else {
                $(".new").removeClass("show")
            }
            return data.rows
        }
        $("#newTel").click(function () {
            var phone = $.trim($("#tel").val())
            $('#myModal').modal('show')
            $("#searchUser").val(phone); console.log($event)
            $event.toSearch(type, phone)
        })
        $("#newName").click(function () {
            var name = $.trim($("#username").val())
            $('#myModal').modal('show')
            $("#searchUser").val(name); console.log($event)
            $event.toSearch(type, name)
        })
        return outParams
    }
    /**
     * columns
     */
    var questionType = function (e) {
        return e.type == "HighriskPregnant" ? "高危孕产妇评测" : e.type == "EPDS" ? "爱丁堡产后抑郁评测" : e.type == "SAS" ? "焦虑自评测" : "自评抑郁"
    }
    var questionOpration = function (e) {
        return '<a class="newC">新做测评</a>&nbsp;&nbsp;<a class="history">历史测评</a>'
    }
    var columnsData = function () {
        var columnsArr = ['userName', 'phoneNum', questionType, 'createTime', questionOpration]
        return $datatableServers.getColumnsData(columnsArr)
    }
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function (setting, json) {
        $("#username,#tel,#fromTim,#toTime").on("input propertychange", function () {
            $datatableUi.table.api().ajax.reload()  // 出发dt的重新加载数据的方法
        })
    }
    /*
    * 行回调函数
    * */
    var rowFn = function (type) {
        var rowCallback = function (row, data, index) {
            $('td:last a', row).click(function () {
                var userId = data.userId
                var attr = $(this).attr("class")
                var obj = {
                    "type": type,
                    "userId": userId
                }
                if (attr == "newC") {
                    if (type == "HighriskPregnant") {
                        $bb.openPage("highRiskPregnantwomen", obj)
                    }
                    if (type == "SAS") {
                        $bb.openPage("anxiety", obj)
                    }
                    if (type == "SDS") {
                        $bb.openPage("depression", obj)
                    }
                    if (type == "EPDS") {
                        $bb.openPage("postpartumDepression", obj)
                    }
                } else {
                    $bb.openPage("pregnantHistory", obj)
                }
            })
        }
        return rowCallback
    }

    /**
     * 获取datatable里的options
     */
    servers.getOptions = function (type) {
        var sessionId = $bb.getSessionStorage("sessionId")
        var options = {
            "ajax": {
                "url": URL + "/patient/evaluation/patientListqry?sessionId=" + sessionId,
                "data": paramsDeal(type),
                "dataSrc": paramsDeal2(type),
            },
            "columns": columnsData(),
            "initComplete": initComplete,
            "rowCallback": rowFn(type)
        }
        return options
    }
    return servers
})